package top.vanchor.myblog2back.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import top.vanchor.myblog2back.entity.BackAdmin;
import top.vanchor.myblog2back.entity.BackAuthGroupRule;
import top.vanchor.myblog2back.entity.BackAuthRule;
import top.vanchor.myblog2back.entity.BlogComment;
import top.vanchor.myblog2back.mapper.BackAuthRuleMapper;
import top.vanchor.myblog2back.service.IBackAuthRuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ding
 * @since 2022-01-27
 */
@Service
public class BackAuthRuleServiceImpl extends ServiceImpl<BackAuthRuleMapper, BackAuthRule> implements IBackAuthRuleService {

    @Resource
    private BackAuthRuleMapper mapper;

    @Override
    public List<BackAuthRule> getAllRules(List<BackAuthGroupRule> groupRules,String roleCode) {
        //根据已有rules获取所拥有的权限主键索引ids
        String ids = "";
        List<BackAuthRule> rules = new ArrayList<>();
        //获取rules
        if ("SUPER_ADMIN".equals(roleCode)){
            //最高权限，获取所有rules
            rules = mapper.getAllRules(ids,null);
            toDoRules(rules);
        }else if ("ADMIN".equals(roleCode)){
            //普通权限，根据拥有权限获取rules
            if (groupRules != null && groupRules.size() > 0){
                ArrayList<Integer> idsList = new ArrayList<>();
                for (BackAuthGroupRule groupRule : groupRules) {
                    ids += (groupRule.getRuleId() + ",");
                    idsList.add(groupRule.getRuleId());
                }
                ids = ids.substring(0, ids.length() - 1);
                rules = mapper.getAllRules(ids,idsList);
                toDoRules(rules);
            }
        }
        return rules;
    }

    public List<BackAuthRule> toDoRules(List<BackAuthRule> list){

        for (BackAuthRule rule : list) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("title",rule.getTitle());
            jsonObject.put("icon",rule.getIcon());
            rule.setMeta(jsonObject);
            if (rule.getChildren()!=null){
                toDoRules(rule.getChildren());
            }
        }
        return list;
    }

    @Override
    public boolean checkIsForbidden(List<Integer> ruleIds) {
//        <select id="checkIsForbidden" resultType="java.lang.Integer">
//                SELECT COUNT (*) FROM back_auth_rule
//        WHERE r_id IN (#{ids})
//        AND back_auth_rule.disabled = 1
//                </select>
        QueryWrapper<BackAuthRule> wrapper = new QueryWrapper<>();
        wrapper.in("r_id",ruleIds)
                .eq("`disabled`",1);
        return mapper.selectCount(wrapper) > 0;
    }

    @Override
    public int doEdit(BackAuthRule bean) {
        if (bean.getRuleId() == null){
            //添加
            return mapper.insert(bean);
        }else{
            //编辑
            QueryWrapper<BackAuthRule> wrapper = new QueryWrapper<>();
            wrapper.eq("r_id",bean.getRuleId());
            return mapper.update(bean,wrapper) + 1;
        }
    }

    @Override
    public int doDeleteBy(List<Integer> ids) {
        int real = 0;
        for (Integer id : ids) {
            //获取所有子节点id
            List<Integer> allIDs = mapper.getDelIdIgnoreThisId(id);
            //把本体也添加进去
            allIDs.add(id);
            //再联系库，把这些ID删除，并返回删除数量
            real += mapper.menuItemDel(allIDs);
        }
        //往回返回
        return real;
    }

    @Override
    public List<Integer> getAllTopRules(Integer ruleId) {
        return mapper.getAllTopRules(ruleId);
    }

    @Override
    public List<BackAuthRule> getAllRulesNoTree(List<BackAuthGroupRule> groupRules, String roleCode) {
        //根据已有rules获取所拥有的权限主键索引ids
        List<BackAuthRule> rules = new ArrayList<>();
        QueryWrapper<BackAuthRule> wrapper = new QueryWrapper<>();
        //获取rules
        if ("SUPER_ADMIN".equals(roleCode)){
            //最高权限，获取所有rules
            rules = mapper.selectList(wrapper);
        }else if ("ADMIN".equals(roleCode)){
            List<Integer> ids = new ArrayList<>();
            for (BackAuthGroupRule g : groupRules) {
                ids.add(g.getRuleId());
            }
            wrapper.in("r_id",ids);
            rules = mapper.selectList(wrapper);
        }
        return rules;
    }

}
